package com.aistar.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: DateUtil
 * @author: LiuWenXiu
 * @date: 2021/10/5 16:32
 * @Description: TODO...本类用来演示:
 */
public class DateUtil {
    /**
     * 将字符串解析成Date类型
     *
     * @param strDate
     * @param pattern
     * @return
     */
    public static Date parse(String strDate, String pattern) {
        if (strDate == null || strDate.trim().length() == 0)
            return null;

        //构建SimpleDateFormat对象
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        Date d = null;

        try {
            d = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 日期转换成字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        //非空判断
        if (date == null)
            return null;
        //构建SimpleDateFormat对象
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        return sdf.format(date);
    }
}
